package com.hluobo.library.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.json.JSONObject;

import com.hluobo.library.model.Announcement;
import com.hluobo.library.service.AnnouncementService;
import com.hluobo.library.util.StreamTool;
import com.opensymphony.xwork2.ActionSupport;

public class AnnouncementAction extends ActionSupport {

	private AnnouncementService announcementService;
	private int resCode = -1;
	private String msg = "err";
	private List<Announcement> announcements = new ArrayList<Announcement>();
	
	public String queryAll(){
		
		announcements = announcementService.queryAll();
		if(announcements.size()>0){
			resCode = 200;
			msg = "查询成功";
		}else{
			resCode = 404;
			msg = "无查询结果";
		}
		
		return SUCCESS;
	}
	
	public String query(){
		
		try {
			JSONObject request = StreamTool.readRequstString();
			int id = request.getInt("id");
			announcements.add(announcementService.queryById(id));
			if(announcements.size()>0){
				resCode = 200;
				msg = "查询成功";
			}else{
				resCode = 404;
				msg = "无查询结果";
			}
		} catch (Throwable e) {
			e.printStackTrace();
		}
		
		return SUCCESS;
	}

	@Resource(name="announcementService")
	public void setAnnouncementService(AnnouncementService announcementService) {
		this.announcementService = announcementService;
	}

	public int getResCode() {
		return resCode;
	}

	public String getMsg() {
		return msg;
	}

	public List<Announcement> getAnnouncements() {
		return announcements;
	}
	
	
	
}
